import logging
import string

log = logging.getLogger('testutils')

def pr(o):
    """Pretty prints the data members of an object"""
    print o
    for i in dir(o):
        if i[:2] == '__' or callable(getattr(o, i)):
            continue
        print '%s: %s' % (i, getattr(o, i))


class DummySock:
    """Dummy socket used for testing."""

    def __init__(self, data_to_read = None):
        if data_to_read is None:
            data_to_read = ''
        self.data_to_read_ = data_to_read
        self.data_sent_ = ''
        self.closed_ = False
        
    def fileno(self):
        return 1

    def recv(self, nbytes):
        data = self.data_to_read_[:bytes]
        self.data_to_read_ = self.data_to_read_[bytes:]
        return data
    
    def send(self, bytes):
        self.data_sent_ = self.data_sent_ + bytes
        bytes = string.replace(bytes, '\n', '\\n')
        bytes = string.replace(bytes, '\t', '\\t')
        log.info('Sent "%s"' % bytes)
        return len(bytes)
        
    def close(self):
        self.closed_ = True
